package com.metal.gear.certification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.metal.gear.certification.entity.po.SysUserRole;
import com.metal.gear.certification.mapper.SysUserRoleMapper;
import com.metal.gear.certification.service.ISysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @Author scott
 * @since 2018-12-21
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Override
    public List<SysUserRole> listByRoleId(String roleId) {
        LambdaQueryWrapper<SysUserRole> wrapper = Wrappers.<SysUserRole>lambdaQuery()
                .eq(SysUserRole::getRoleId, roleId);
        return list(wrapper);
    }

    @Override
    public List<String> listUserIdByRoleId(String roleId) {
        return listByRoleId(roleId).stream().map(SysUserRole::getUserId).collect(Collectors.toList());
    }

    @Override
    public List<SysUserRole> listByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> wrapper = Wrappers.<SysUserRole>lambdaQuery()
                .eq(SysUserRole::getUserId, userId);
        return list(wrapper);
    }


    @Override
    public List<String> listRoleIdByUserId(String userId) {
        return listByUserId(userId).stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());
    }

    @Override
    public Boolean deleteByUserIdAndRoleId(String userId, String roleId) {
        LambdaQueryWrapper<SysUserRole> wrapper = Wrappers.<SysUserRole>lambdaQuery()
                .eq(SysUserRole::getUserId, userId)
                .eq(SysUserRole::getRoleId,roleId);
        return remove(wrapper);
    }

    @Override
    public Boolean deleteByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> wrapper = Wrappers.<SysUserRole>lambdaQuery()
                .eq(SysUserRole::getUserId, userId);
        return remove(wrapper);
    }
}
